package com.fin.zw.intelligentbot.service;

import com.fin.zw.intelligentbot.entity.*;
import com.fin.zw.intelligentbot.model.request.VerbalNodeRequest;
import com.fin.zw.intelligentbot.model.session.SessionModel;
import com.fin.zw.intelligentbot.repository.CategoryLabelRepository;
import com.fin.zw.intelligentbot.repository.CorpusRepository;
import com.fin.zw.intelligentbot.repository.VerbalNodeRepository;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.querydsl.core.BooleanBuilder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;

@Service("verbalNodeService")
public class VerbalNodeService {

    private final static Logger log = LoggerFactory.getLogger(VerbalNodeService.class);

    @Autowired
    private VerbalNodeRepository verbalNodeRepository;

    @Autowired
    private CategoryLabelRepository categoryLabelRepository;

    @Autowired
    private CorpusRepository corpusRepository;

    @Autowired
    private CommonService commonService;

    /**
     * 话术节点信息查询
     *
     * @param pageable
     * @param request
     * @return
     */
    public Page<VerbalNode> queryNode(Pageable pageable, VerbalNodeRequest request, SessionModel sessionModel) {
        BooleanBuilder builder = new BooleanBuilder();
        QVerbalNode qVerbalNode = QVerbalNode.verbalNode;
        if(StringUtils.isNotBlank(sessionModel.getCompanyId())){
            builder.and(qVerbalNode.company.id.eq(sessionModel.getCompanyId()));
        }
        if (StringUtils.isNotBlank(request.getVerbalId())) {
            builder.and(qVerbalNode.verbalId.eq(request.getVerbalId()));
        }
        if (request.getFlowSign() != null) {
            builder.and(qVerbalNode.flowSign.eq(request.getFlowSign()));
        }
        Page<VerbalNode> page = verbalNodeRepository.findAll(builder, pageable);
        return page;
    }

    /**
     * 查询当前企业所有的话术节点
     * @param sessionModel
     * @return
     */
    public List<VerbalNode> getVerbalNode( SessionModel sessionModel){
        BooleanBuilder builder = new BooleanBuilder();
        QVerbalNode qVerbalNode = QVerbalNode.verbalNode;
        if(StringUtils.isNotBlank(sessionModel.getCompanyId())){
            builder.and(qVerbalNode.company.id.eq(sessionModel.getCompanyId()));
        }
        Iterator<VerbalNode> iterator = verbalNodeRepository.findAll(builder).iterator();
        return Lists.newArrayList(iterator);
    }
    /**
     * 话术节点信息添加
     *
     * @param request
     * @param sessionModel
     */
    public void createNode(VerbalNodeRequest request, SessionModel sessionModel) {
        VerbalNode verbalNode = new VerbalNode();
        BeanUtils.copyProperties(request, verbalNode);
        Company company = commonService.getCompany(sessionModel);
        verbalNode.setCompany(company);
        setNode(request, verbalNode);
        verbalNodeRepository.save(verbalNode);
    }

    public void setNode(VerbalNodeRequest request, VerbalNode verbalNode) {
        if (StringUtils.isNotBlank(request.getCategoryId())) {
            Optional<CategoryLabel> optional = categoryLabelRepository.findById(request.getCategoryId());
            if (optional.isPresent()) {
                verbalNode.setAttitude(optional.get());
            }
        }
        if (request.getCorpusIds() != null && !request.getCorpusIds().isEmpty()) {
            Iterator<CorpusInfo> infoIterator = corpusRepository.findAll(QCorpusInfo.corpusInfo.id.in(request.getCorpusIds())).iterator();
            Set<CorpusInfo> set = Sets.newHashSet(infoIterator);
            verbalNode.setCorpusInfoSet(set);
        }
    }


    /**
     * 话术节点信息的更新
     *
     * @param request
     */
    public void updateNode(VerbalNodeRequest request) {
        Optional<VerbalNode> optional = verbalNodeRepository.findById(request.getId());
        if (!optional.isPresent()) {
            throw new RuntimeException("修改的节点信息不存在！");
        }
        VerbalNode verbalNode = optional.get();
        BeanUtils.copyProperties(request, verbalNode);
        setNode(request, verbalNode);
        verbalNodeRepository.save(verbalNode);
    }

    /**
     * 话术节点信息的删除
     *
     * @param id
     */
    public void deleteNode(String id) {
        verbalNodeRepository.deleteById(id);
    }
}
